<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.bookmanager.model.User" %>
<%@ page import="com.bookmanager.model.Favorite" %>
<%@ page import="java.util.List" %>
<%
    User user = (User) session.getAttribute("user");
    if (user == null) {
        response.sendRedirect("../login.jsp");
        return;
    }

    // 检查所有可能的属性
    List<Favorite> favorites1 = (List<Favorite>) request.getAttribute("favorites");
    List<Favorite> favorites2 = (List<Favorite>) session.getAttribute("favorites");

    // 优先使用request中的属性
    List<Favorite> favorites = favorites1;
    if (favorites == null) {
        favorites = favorites2;
        }

    if (favorites != null) {
        for (Favorite favorite : favorites) {
            }
    }
%>
<html>
<head>
    <title>我的收藏 - 图书管理系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: #f5f7fa;
            color: #333;
        }

        .header {
            background: white;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            padding: 0 20px;
        }

        .nav-bar {
            display: flex;
            justify-content: space-between;
            align-items: center;
            max-width: 1200px;
            margin: 0 auto;
            height: 70px;
        }

        .logo {
            font-size: 1.5rem;
            font-weight: bold;
            color: #667eea;
        }

        .user-info {
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .welcome {
            color: #666;
        }

        .logout-btn {
            padding: 8px 16px;
            background: #dc3545;
            color: white;
            text-decoration: none;
            border-radius: 5px;
            font-size: 0.9rem;
        }

        .container {
            max-width: 1200px;
            margin: 30px auto;
            padding: 0 20px;
        }

        .page-header {
            text-align: center;
            margin-bottom: 40px;
        }

        .page-header h1 {
            color: #333;
            margin-bottom: 10px;
        }

        .page-header p {
            color: #666;
            font-size: 1.1rem;
        }

        .section {
            background: white;
            border-radius: 10px;
            padding: 30px;
            margin-bottom: 30px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
        }

        .section-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
            padding-bottom: 15px;
            border-bottom: 2px solid #f0f0f0;
        }

        .section-title {
            font-size: 1.3rem;
            color: #333;
        }

        .favorites-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
            gap: 20px;
            margin-top: 20px;
        }

        .favorite-card {
            background: #f8f9fa;
            border-radius: 10px;
            padding: 20px;
            border: 1px solid #e9ecef;
            transition: all 0.3s ease;
        }

        .favorite-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
        }

        .book-icon {
            font-size: 3rem;
            text-align: center;
            margin-bottom: 15px;
            color: #667eea;
        }

        .book-title {
            font-size: 1.1rem;
            font-weight: 600;
            margin-bottom: 8px;
            color: #333;
        }

        .book-author {
            color: #666;
            margin-bottom: 10px;
            font-size: 0.9rem;
        }

        .favorite-date {
            color: #999;
            font-size: 0.8rem;
            margin-bottom: 15px;
        }

        .action-buttons {
            display: flex;
            gap: 10px;
        }

        .btn {
            padding: 8px 12px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 0.85rem;
            text-decoration: none;
            display: inline-block;
            text-align: center;
            flex: 1;
        }

        .btn-detail {
            background: #17a2b8;
            color: white;
        }

        .btn-remove {
            background: #dc3545;
            color: white;
        }

        .btn-back {
            background: #667eea;
            color: white;
            padding: 10px 20px;
            text-decoration: none;
            border-radius: 5px;
            display: inline-block;
            margin-top: 20px;
        }

        .empty-state {
            text-align: center;
            padding: 60px 20px;
            color: #666;
        }

        .empty-icon {
            font-size: 4rem;
            margin-bottom: 20px;
            opacity: 0.5;
        }

        .stats {
            display: flex;
            gap: 20px;
            margin-bottom: 20px;
        }

        .stat-item {
            background: white;
            padding: 15px;
            border-radius: 8px;
            text-align: center;
            flex: 1;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }

        .stat-number {
            font-size: 1.5rem;
            font-weight: bold;
            color: #667eea;
        }

        .stat-label {
            color: #666;
            font-size: 0.9rem;
        }
        .btn-favorite {
            background: #e83e8c;
            color: white;
            padding: 5px 10px;
            border: none;
            border-radius: 3px;
            cursor: pointer;
            font-size: 0.85rem;
        }

        .btn-favorite:hover {
            background: #d91a72;
        }
    </style>
</head>
<body>
<!-- 顶部导航 -->
<div class="header">
    <div class="nav-bar">
        <div class="logo">📚 图书管理系统</div>
        <div class="user-info">
            <span class="welcome">欢迎，<strong><%= user.getUsername() %></strong></span>
            <a href="<%= request.getContextPath() %>/logout" class="logout-btn">退出登录</a>
        </div>
    </div>
</div>

<div class="container">
    <!-- 页面头部 -->
    <div class="page-header">
        <h1>⭐ 我的收藏</h1>
        <p>管理您喜欢的图书收藏</p>
    </div>

    <!-- 收藏统计 -->
    <div class="stats">
        <div class="stat-item">
            <div class="stat-number"><%= favorites != null ? favorites.size() : 0 %></div>
            <div class="stat-label">收藏数量</div>
        </div>
    </div>

    <!-- 收藏列表 -->
    <div class="section">
        <div class="section-header">
            <h3 class="section-title">收藏的图书</h3>
        </div>

        <% if (favorites != null && !favorites.isEmpty()) { %>
        <div class="favorites-grid">
            <% for (Favorite favorite : favorites) { %>
            <div class="favorite-card">
                <div class="book-icon">📖</div>
                <div class="book-title"><%= favorite.getBookTitle() != null ? favorite.getBookTitle() : "未知图书" %></div>
                <div class="book-author"><%= favorite.getBookAuthor() != null ? favorite.getBookAuthor() : "未知作者" %></div>
                <div class="favorite-date">收藏于：<%= favorite.getCreateTime() != null ? favorite.getCreateTime().toLocalDate().toString() : "未知" %></div>
                <div class="action-buttons">
                    <button class="btn btn-detail" onclick="showBookDetail(<%= favorite.getBookId() %>)">查看详情</button>
                    <button class="btn btn-remove" onclick="removeFavorite(<%= favorite.getBookId() %>)">取消收藏</button>
                </div>
            </div>
            <% } %>
        </div>
        <% } else { %>
        <div class="empty-state">
            <div class="empty-icon">⭐</div>
            <h3>暂无收藏图书</h3>
            <p>快去发现并收藏您喜欢的图书吧！</p>
            <a href="../books" class="btn-back">去发现图书</a>
        </div>
        <% } %>
    </div>

    <div style="text-align: center;">
        <a href="<%= request.getContextPath() %>/dashboard" class="btn-back">返回用户中心</a>
    </div>
</div>

<script>
    function showBookDetail(bookId) {
        window.location.href = '../book-detail?bookId=' + bookId;
    }

    function removeFavorite(bookId) {
        if (confirm('确定要取消收藏这本书吗？')) {
            const form = document.createElement('form');
            form.method = 'POST';
            form.action = '../favorite';

            const actionInput = document.createElement('input');
            actionInput.type = 'hidden';
            actionInput.name = 'action';
            actionInput.value = 'remove';
            form.appendChild(actionInput);

            const bookIdInput = document.createElement('input');
            bookIdInput.type = 'hidden';
            bookIdInput.name = 'bookId';
            bookIdInput.value = bookId;
            form.appendChild(bookIdInput);

            document.body.appendChild(form);
            form.submit();
        }
    }
</script>
</body>
</html>